package com.shensi.adminCollect.controller.customer;

import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.common.utils.StringUtils;
import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.common.utils.sign.Md5Utils;
import com.shensi.adminCollect.domain.customer.CustomerElectronicCard;
import com.shensi.adminCollect.domain.customer.IcRegisteInfo;
import com.shensi.adminCollect.service.customer.ICustomerElectronicCardService;
import com.shensi.adminCollect.service.customer.IIcRegisteInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;

/**
 * 卡编号Controller
 * 
 * @author qujia
 * @date 2021-11-08
 */
@RestController
@RequestMapping("/customer/ic")
public class IcRegisteInfoController extends BaseController
{
    @Autowired
    private IIcRegisteInfoService icRegisteInfoService;

    @Autowired
    private ICustomerElectronicCardService customerElectronicCardService;




    /**
     * 查询卡编号列表
     */
    @GetMapping("/list")
    @ApiOperation("获取ic卡集合")
    public TableDataInfo list(IcRegisteInfo icRegisteInfo)
    {
        startPage();
        LoginUser loginUser=SecurityUtils.getLoginUser();
        icRegisteInfo.setEnterpriseId(loginUser.getUser().getEnterpriseId());
        List<IcRegisteInfo> list = icRegisteInfoService.selectIcRegisteInfoList(icRegisteInfo);
        return getDataTable(list);
    }

    @GetMapping("/statisticsInfo")
    @ApiOperation("统计开卡人数以及总额")
    public AjaxResult statisticsInfo(IcRegisteInfo icRegisteInfo)
    {
        return AjaxResult.success(icRegisteInfoService.selectstatisticsInfo(icRegisteInfo));
    }
    /**
     * 导出卡编号列表
     */
    @GetMapping("/export")
    public AjaxResult export(IcRegisteInfo icRegisteInfo)
    {
        List<IcRegisteInfo> list = icRegisteInfoService.selectIcRegisteInfoList(icRegisteInfo);
        ExcelUtil<IcRegisteInfo> util = new ExcelUtil<IcRegisteInfo>(IcRegisteInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 修改密码
     */
    @PostMapping(value = "/resetpwd")
    public AjaxResult Resetpwd(@RequestBody IcRegisteInfo icRegisteInfo)
    {
        if(icRegisteInfo.getPassword().length() ==0)
        {
            return  AjaxResult.error("请输入密码");
        }
        icRegisteInfo.setPassword(Md5Utils.hash(icRegisteInfo.getPassword()));
        return toAjax(icRegisteInfoService.updatePasswd(icRegisteInfo));
    }

    /**
     * 现金充值U
     */
    @PostMapping(value = "/updateRecharge")
    public AjaxResult UpdateRecharge(@RequestBody IcRegisteInfo icRegisteInfo)
    {
        if(icRegisteInfo.getAmt()==null || icRegisteInfo.getAmt().equals(0.00))
        {
            return AjaxResult.error("请输入金额");
        }
        if(icRegisteInfo.getStationId() == null)
        {
            return AjaxResult.error("请输入消费的油站");
        }
        if(StringUtils.isBlank(icRegisteInfo.getIcCardno()))
        {
            return AjaxResult.error("请输入卡号");
        }
        icRegisteInfo.setrechargeType(1);
        return toAjax(icRegisteInfoService.updateRechargeTran(icRegisteInfo));
    }

    /**
     * 现金充值U
     */
    @PostMapping(value = "/UpdateRechargeByAfter")
    public AjaxResult UpdateRechargeByAfter(@RequestBody IcRegisteInfo icRegisteInfo)
    {
        if(icRegisteInfo.getAmt()==null || icRegisteInfo.getAmt().equals(0.00))
        {
            return AjaxResult.error("请输入金额");
        }
        if(icRegisteInfo.getStationId() == null)
        {
            return AjaxResult.error("请输入消费的油站");
        }
        if(StringUtils.isBlank(icRegisteInfo.getIcCardno()))
        {
            return AjaxResult.error("请输入卡号");
        }
        icRegisteInfo.setrechargeType(1);
        return toAjax(icRegisteInfoService.updateRechargeTran(icRegisteInfo));
    }

    /**
     * 现金消费
     */
    @PostMapping(value = "/updateconsumption")
    public AjaxResult updateconsumption(@RequestBody IcRegisteInfo icRegisteInfoReq)
    {
        if(icRegisteInfoReq.getAmt()==null || icRegisteInfoReq.getAmt().equals(0.00))
        {
            return AjaxResult.error("请输入金额");
        }
        if(StringUtils.isBlank(icRegisteInfoReq.getIcCardno()))
        {
            return AjaxResult.error("请输入卡号");
        }
        if(icRegisteInfoReq.getStationId() == null)
        {
            return AjaxResult.error("请输入消费的油站");
        }
        IcRegisteInfo icRegisteInfo = icRegisteInfoService.selectIcRegisteInfoById(icRegisteInfoReq.getIcCardno());

        if(icRegisteInfo == null){
            return AjaxResult.error("未查到此卡");
        }
        CustomerElectronicCard queryCard = new CustomerElectronicCard();
        queryCard.setStationId(icRegisteInfo.getEnterpriseId()); // 电子卡油站id存的油企。。。。。。。很服气。
        queryCard.setCardOilsType(icRegisteInfo.getCardType());
        queryCard.setMobilePhone(icRegisteInfo.getIcContactsphone());

        List<CustomerElectronicCard> customerElectronicCards = customerElectronicCardService.selectCustomerElectronicCardList(queryCard);
        if(customerElectronicCards.size() != 1){
            return AjaxResult.error("用户电子卡数据异常");
        }

        int i = customerElectronicCards.get(0).getAmt().compareTo(icRegisteInfoReq.getAmt());
        if(i == -1)
        {
            return AjaxResult.error("余额不足");
        }
        icRegisteInfoReq.setEnterpriseId(icRegisteInfo.getEnterpriseId());
        return toAjax(icRegisteInfoService.updateconsumptionTran(icRegisteInfoReq, customerElectronicCards.get(0)));
    }

    /**
     * 现金消费
     */
    @PostMapping(value = "/updateconsumptionByAfter")
    public void updateconsumptionByAfter(@RequestBody IcRegisteInfo icRegisteInfo, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/customer/ic/updateconsumption").forward(request, response);
    }
    /**
     * 修改密码
     */
    @PostMapping(value = "/updateIcRegisteInfo")
    public AjaxResult updateIcRegisteInfo(@RequestBody IcRegisteInfo icRegisteInfo)
    {
        return toAjax(icRegisteInfoService.updateIcRegisteInfo(icRegisteInfo));
    }
    /**
     * 修改卡编号
     */
    @PutMapping(value = "put")
    public AjaxResult edit(@RequestBody IcRegisteInfo icRegisteInfo)
    {
        return toAjax(icRegisteInfoService.updateIcRegisteInfo(icRegisteInfo));
    }

    @PostMapping(value = "/updatecancellation")
    public AjaxResult updatecancellation(@RequestBody IcRegisteInfo icRegisteInfo)
    {
        return toAjax(icRegisteInfoService.deleteIcRegisteInfoById(icRegisteInfo.getIcCardno()));
    }
    /**
     * 获取卡编号详细信息
     */
    @GetMapping(value = "/getcardno/{icCardno}")
    @ApiOperation("获取ic卡详情")
    public AjaxResult getInfo(@PathVariable("icCardno") String icCardno)
    {
        IcRegisteInfo icRegisteInfo=new IcRegisteInfo();
        icRegisteInfo.setIcCardno(icCardno);
         List<IcRegisteInfo> icRegisteInfos= icRegisteInfoService.selectIcRegisteInfoList(icRegisteInfo);
         if(icRegisteInfos==null||icRegisteInfos.size()<=0){
             return AjaxResult.error("没有当前实体卡信息");
         }
        return AjaxResult.success(icRegisteInfos.get(0));
    }

    /**
     * 新增卡编号
     */
    @PostMapping("/save")
    @ApiOperation("新增IC卡")
    @Transactional
    public AjaxResult add(@RequestBody IcRegisteInfo icRegisteInfo)
    {

        if(icRegisteInfo.getIcCardno() == null){
            return AjaxResult.error("请传卡号");
        }
        if(icRegisteInfo.getEnterpriseId() == null){
            return AjaxResult.error("请传油企id");
        }
        if(icRegisteInfo.getCardType() == null){
            return AjaxResult.error("请传卡类型");
        }
        if(StringUtils.isEmpty(icRegisteInfo.getIcContactsphone())){
            return AjaxResult.error("请传手机号");
        }

        // 查重
        IcRegisteInfo queryIc = new IcRegisteInfo();
        queryIc.setIcContactsphone(icRegisteInfo.getIcContactsphone());
        queryIc.setCardType(icRegisteInfo.getCardType());
//        queryIc.setStationId(icRegisteInfo.getStationId());
        queryIc.setEnterpriseId(icRegisteInfo.getEnterpriseId());
        List<IcRegisteInfo> list = icRegisteInfoService.selectIcRegisteInfoList(queryIc);
        if(!list.isEmpty()){
            return AjaxResult.error("该用户已经创建该类型卡片");
        }

        CustomerElectronicCard queryCard = new CustomerElectronicCard();
        queryCard.setStationId(icRegisteInfo.getEnterpriseId()); // 油站存油企。别忘了
        queryCard.setCardOilsType(icRegisteInfo.getCardType());
        queryCard.setMobilePhone(icRegisteInfo.getIcContactsphone());


        List<CustomerElectronicCard> customerElectronicCards = customerElectronicCardService.selectCustomerElectronicCardList(queryCard);

        if(customerElectronicCards.size() > 1){
            return AjaxResult.error("用户电子卡数据异常");
        }

        if(StringUtils.isNotBlank(icRegisteInfo.getPassword())) {
            icRegisteInfo.setPassword(Md5Utils.hash(icRegisteInfo.getPassword()));
        } else {
            icRegisteInfo.setPassword(Md5Utils.hash("666666"));
        }


        icRegisteInfoService.insertIcRegisteInfo(icRegisteInfo);

        if(customerElectronicCards.size() == 0 ){
            Long dzkId = icRegisteInfoService.createUserElectronic(icRegisteInfo.getIcCardno(), null, null);
            icRegisteInfo.setDzkId(dzkId);
        }else{
            icRegisteInfo.setDzkId(customerElectronicCards.get(0).getId());
        }

        return AjaxResult.success("新增成功");
    }



}
